package com.topscomm.file.convert.aspose;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.core.FileFormatEnum;
import com.topscomm.file.util.FileUtil;

/**
 * 转pdf文件
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月15日下午7:27:04
 * @modify:
 */
public class AsposeFileConvertToHtml {
	/**
	 * 转换文件
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:26:46
	 * @modify:
	 */
	public static String fileToHtml(String identifier, String filePath, String targetPath) throws Exception {
		String mimeType = FileUtil.getMimeType(filePath);
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.WORD.mimeType)) {
			return wordToHtml(identifier, filePath, targetPath);
		}
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.EXCEL.mimeType)) {
			return excelToHtml(identifier, filePath, targetPath);
		}
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.PPT.mimeType)) {
			return pptToHtml(identifier, filePath, targetPath);
		}
		if (FileUtil.matchMimeType(mimeType, FileFormatEnum.PDF.mimeType)) {
			return pdfToHtml(identifier, filePath, targetPath);
		}
		return null;
	}

	/**
	 * @description:pdf转html
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @throws Exception
	 * @author: wanghongwei
	 * @date: 2021-3-19 11:27:04
	 * @modify:
	 */
	private static String pdfToHtml(String identifier, String filePath, String targetPath) throws Exception {
		AsposeSetLicense.setPdfAsposeLicense();
		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String htmlfFilePath = targetPath + "/" + fileName + ".html";
		com.aspose.pdf.Document doc = null;
		try {
			doc = new com.aspose.pdf.Document(filePath);
			com.aspose.pdf.HtmlSaveOptions options = new com.aspose.pdf.HtmlSaveOptions(com.aspose.pdf.SaveFormat.Html);
			options.setFixedLayout(true);
			options.setSplitIntoPages(false);
			doc.save(htmlfFilePath, options);
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			if (doc != null) {
				doc.dispose();
			}
		}
		return htmlfFilePath;
	}

	/**
	 * word转pdf
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:01:20
	 * @modify:
	 */
	private static String wordToHtml(String identifier, String filePath, String targetPath) throws Exception {
		// AsposeSetLicense.setWordAsposeLicense();
		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String htmlfFilePath = targetPath + "/" + fileName + ".html";
		com.aspose.words.Document doc = null;
		try {
			doc = new com.aspose.words.Document(filePath);
			com.aspose.words.HtmlSaveOptions options = new com.aspose.words.HtmlSaveOptions(
					com.aspose.words.SaveFormat.HTML);
			options.setExportPageMargins(true);
			options.setImageResolution(200);
			doc.save(htmlfFilePath, options);
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			if (doc != null) {
				doc.cleanup();
			}
		}
		return htmlfFilePath;
	}

	/**
	 * excel转pdf
	 * 
	 * @description:
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @author: donghang
	 * @throws Exception
	 * @date: 2021年3月15日下午7:01:41
	 * @modify:
	 */
	private static String excelToHtml(String identifier, String filePath, String targetPath) throws Exception {
		// AsposeSetLicense.setExcelAsposeLicense();
		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String htmlfFilePath = targetPath + "/" + fileName + ".html";
		com.aspose.cells.Workbook wb = null;
		try {
			wb = new com.aspose.cells.Workbook(filePath);
			com.aspose.cells.HtmlSaveOptions options = new com.aspose.cells.HtmlSaveOptions(
					com.aspose.cells.SaveFormat.HTML);
			options.setAddTooltipText(true);
			options.setExportHiddenWorksheet(false);
			wb.calculateFormula();
			wb.save(htmlfFilePath, options);
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			if (wb != null) {
				wb.dispose();
			}
		}
		return htmlfFilePath;
	}

	/**
	 * @description: ppt转pdf
	 * @param filePath
	 * @param targetPath
	 * @return
	 * @throws Exception
	 * @author: wanghongwei
	 * @date: 2021-3-19 11:27:04
	 * @modify:
	 */
	private static String pptToHtml(String identifier, String filePath, String targetPath) throws Exception {
		// AsposeSetLicense.setPowerPointAsposeLicense();
		String fileName = targetPath.substring(targetPath.lastIndexOf("/") + 1);
		String htmlfFilePath = targetPath + "/" + fileName + ".html";
		com.aspose.slides.Presentation presentation = null;
		try {
			presentation = new com.aspose.slides.Presentation(filePath);
			com.aspose.slides.HtmlOptions options = new com.aspose.slides.HtmlOptions();
			options.setShowHiddenSlides(false);
			presentation.save(htmlfFilePath, com.aspose.slides.SaveFormat.Html, options);
		} catch (Exception e) {
			throw new ServiceException(e);
		} finally {
			if (presentation != null) {
				presentation.dispose();
			}
		}
		return htmlfFilePath;
	}
}
